﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Windows.Input;

namespace ViewMaker.Core.Controls
{

    public class CanvasViewControl : PanelViewControl
    {

        #region ShouldSerialize

        public override bool ShouldSerializeHeaderWidth() { return true; }
        public override bool ShouldSerializeItemWidth() { return true; }
        //public override bool ShouldSerializeWidth() { return true; }
        //public override bool ShouldSerializeHeight() { return true; }

        #endregion


        public CanvasViewControl()
        {
            ControlType = ViewControlType.Canvas;
            IsPanel = true;
            HeaderWidth = ViewMakerSettings.Instance.DefaultCanvasHeaderWidth;
            ItemWidth = ViewMakerSettings.Instance.DefaultCanvasItemWidth;
            HeaderPosition = LayoutHeaderPosition.Left;
        }

        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            XamlNode panel = CreatePanel(parent, "Canvas");
            int baseY = 0;

            foreach (var child in Item.Children)
            {
                var headerItem = child.Element as HeaderViewControl;
                child.Control.CanvasLeft = child.Control.CanvasLeft ?? 0;
                child.Control.CanvasTop = child.Control.CanvasTop ?? baseY;
                int x = child.Control.CanvasLeft.Value;
                int y = child.Control.CanvasTop.Value;

                if (headerItem != null)
                {
                    foreach (var item in CreateHeaderControl(child, panel))
                    {
                        panel.AppendChild(item);
                        SetCanvasLeftTop(item, x, y);
                        x += child.Control.Width ?? ItemWidth ?? ViewMakerSettings.Instance.CanvasHorizontalOffset;
                    }
                    baseY += child.Control.Height ?? ViewMakerSettings.Instance.CanvasVerticalOffset;
                    if (headerItem.ActualHeaderPosition == LayoutHeaderPosition.Top) baseY += ViewMakerSettings.Instance.CanvasVerticalOffset;
                }
                else
                {
                    foreach (var item in child.CreateXaml(panel))
                    {
                        SetItemAttribute(child, item);
                        panel.AppendChild(item);
                        SetCanvasLeftTop(item, x, y);
                        x += child.Control.Width ?? ItemWidth ?? ViewMakerSettings.Instance.CanvasHorizontalOffset;
                    }
                    baseY += child.Control.Height ?? ViewMakerSettings.Instance.CanvasVerticalOffset;
                }
            }

            SetBindingAttribute(panel, "DataContext", Item.BindPath, true);
            ApplyBaseProperty(panel);
            var list = new List<XamlNode>();
            list.Add(Decoration ?? panel);
            return list;
        }

        private void SetCanvasLeftTop(XamlNode item, int x, int y)
        {
            item.SetAttribute("Canvas.Left", x.ToString());
            item.SetAttribute("Canvas.Top", y.ToString());
        }

    }

}
